package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存或者修改地址信息
    @Override
    public Boolean updateLocation(Long userId, Double latitude, Double longitude, String addrStr) {
        try {
            Query query = Query.query(Criteria.where("userId").is(userId));
            GeoJsonPoint gsp = new GeoJsonPoint(longitude,latitude);
            //如果地址信息存在 则修改信息 如果不存在 则添加信息
            if (mongoTemplate.exists(query,UserLocation.class)){
                Update update = Update.update("location",gsp)
                        .set("address",addrStr)
                        .set("updated",System.currentTimeMillis())
                        .set("lastUpdated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLocation.class);
            }else {
                //如果不存在创建对象并保存
                UserLocation userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setLocation(gsp);
                userLocation.setAddress(addrStr);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());

                mongoTemplate.save(userLocation);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //搜索附近的人
    @Override
    public List<Long> searchNearUser(Long userId, double parseDouble) {
        //查询用户定位信息
        UserLocation userLocation = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)), UserLocation.class);

        //构造坐标点
        GeoJsonPoint point = userLocation.getLocation();
        //构造半径
        Distance distanceObj = new Distance(parseDouble / 1000 , Metrics.KILOMETERS);
        //画一个圆圈
        Circle circle = new Circle(point,distanceObj);
        //构造query对象
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        //返回数据
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);
        List<Long> ids = userLocations.stream().map(UserLocation::getUserId).collect(Collectors.toList());

        return ids;
    }
}
